/**
 * 
 */
package mycsveditor.editors;

import org.eclipse.jface.text.rules.ICharacterScanner;
import org.eclipse.jface.text.rules.IPredicateRule;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.Token;

/**
 * @author dongxu
 * 
 */
public class CSVJavaCallRule implements IPredicateRule {

	protected IToken fToken;

	public CSVJavaCallRule(IToken token) {
		this.fToken = token;
	}

	/**
	 * @see org.eclipse.jface.text.rules.IRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner)
	 */
	@Override
	public IToken evaluate(ICharacterScanner scanner) {
		return evaluate(scanner, false);
	}

	/**
	 * @see org.eclipse.jface.text.rules.IPredicateRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner,
	 *      boolean)
	 */
	@Override
	public IToken evaluate(ICharacterScanner scanner, boolean resume) {
		boolean quoted = false;
		int prev = 0;
		int curr = scanner.read();
		if (scanner.getColumn() == 1) {
			while ((curr != '\n' || quoted) && curr != ICharacterScanner.EOF) {
				if (curr == '\"' && prev != '\\')
					quoted = quoted ? false : true;
				prev = curr;
				curr = scanner.read();
			}
			if (curr == ICharacterScanner.EOF) {
//				return Token.EOF;
				scanner.unread();
			}
			return fToken;
		}
		scanner.unread();
		return Token.UNDEFINED;
	}

	/**
	 * @see org.eclipse.jface.text.rules.IPredicateRule#getSuccessToken()
	 */
	@Override
	public IToken getSuccessToken() {
		return fToken;
	}

}
